import ConfigParser
import sys
import os
import datetime

if (len(sys.argv) < 2):
	print "Script requires conf file as argument"
	sys.exit()

inconf = sys.argv[1]
Config = ConfigParser.ConfigParser()
Config.read(inconf)
"""
pathway_list:/Users/jake/Downloads/metanodes_afux/pathways.txt
sif_root=/Users/jake/Downloads/pathways_afux
metanode_root=/Users/jake/Downloads/metanodes_afux
webapp_root=/Users/jake/local/apache-tomcat-6.0.35/webapps/ROOT/systemsbiology

cd /Users/jake/local/apache-tomcat-6.0.35/webapps/ROOT/systemsbiology/images
mv ~/Downloads/metanodes_afux/VitD .
python resizeMetanodes2Icon.py /Users/jake/local/apache-tomcat-6.0.35/webapps/ROOT/systemsbiology/images/VitD 75 75_75
cd ../python
python makeNodesEdges.py VitD ~/Downloads/pathways_afux/VitD/VitD.sif ~/Downloads/pathways_afux/VitD/VitD_Edge_Config.tsv 

"""
proj = Config.get("datasource_path", "project")
sif_root = Config.get("datasource_path", "sif_root")
metanode_root = Config.get("datasource_path", "metanode_root")
webapp_root = Config.get("datasource_path", "webapp_root")
pathway_inf = Config.get("datasource_path", "pathway_list")
pyscript_path = Config.get("datasource_path", "pyscript_path")
python_path = Config.get("datasource_path", "python_path")

image_quality = Config.get("metanode_icon", "image_quality")
x_y_size = Config.get("metanode_icon", "x_y_size")

#[rxns]
#account_multiple:1
rxn_multiple = Config.get("rxns", "account_multiple")

print "\nProcessing pathways from" + pathway_inf + " " + str(datetime.datetime.now())

pf = open(pathway_inf, "r")
for p in pf.readlines():
	p = p.strip()
	if (len(p) == 0):
		continue
	psh = open(p+".sh", "w")
	#cp images
	if (not os.path.exists("%s/images/%s/%s\n" %(webapp_root,proj,p))):
		psh.write("mkdir %s/images/%s/%s\n" %(webapp_root,proj,p))
	psh.write("cp %s/%s/* %s/images/%s/%s\n" %(metanode_root, p, webapp_root, proj, p))
	psh.write("rm -rf %s/images/%s/%s/*icon*\n" %(webapp_root,proj,p))
	psh.write("rm -rf %s/images/%s/%s/Thumbs.db\n" %(webapp_root,proj,p))
	#make icons
	psh.write("%s %s/python/resizeMetanodes2Icon.py %s/images/%s/%s %s %s\n" %(python_path, webapp_root, webapp_root, proj, p, image_quality, x_y_size))
	#add multiple for rxns	
	psh.write("%s %s/python/makeNodesEdges.py %s %s %s/%s/%s.sif %s/%s/%s_Edge_Config.tsv %s\n" %(python_path,webapp_root,proj,p,sif_root,p,p,sif_root,p,p,rxn_multiple))
	psh.close()
	os.system("sh " + p+".sh")
	os.system("rm " + p+".sh")	
pf.close()

print "\nFinished processing pathways from " + proj + " " + str(datetime.datetime.now()) + " If pathway(s) are not integrated into webapp, then add the aboved lines inside html <select></select> \n<option size='+0' value='Pathway_sif'>Pathway (SIF)</option>"
